package Behavioral.Observer.pull;

import java.util.concurrent.*;

//模拟客户端
public class Client {

    private Server server;

    private ClientData data=new ClientData();

    public ClientData getData() {
        return data;
    }

    public void setData(ClientData data) {
        this.data = data;
    }

    public void setServer(Server server) {
        this.server = server;
    }

    //    定时拉取服务器的数据，如果有更新就刷新数据
    public void scheduledPullData(){
//        轮询
        ScheduledThreadPoolExecutor service = new ScheduledThreadPoolExecutor(1);
//        scheduleAtFixedRate才能实现周期性的执行
//        schedule只会执行一次
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(1);
                server.accept(Client.this);
            }
        },10,10,TimeUnit.SECONDS);
    }

    public void changeClentData(String data){
        this.data.setData(data);
    }

}
